package cn.me.alphamq.broker.model.timewheel;

import cn.me.alphamq.broker.constant.BrokerConstant;
import cn.me.alphamq.broker.entity.RetryConsumeMsg;
import cn.me.alphamq.broker.enumeration.SlotMsgTypeEnum;
import cn.me.alphamq.common.entity.req.PushDelayMsgReq;
import cn.me.alphamq.common.util.DelayTimeUtil;

/**
 * 延迟消息的模型
 *
 * @author f
 */
public class DelayMessageModel {

    /**
     * 消息
     */
    private Object msg;

    /**
     * 消息的类型
     *
     * @see SlotMsgTypeEnum
     */
    private int msgType;

    /**
     * 处理的时间，单位：ms，= 当前时间 + 延迟时间 * 1000
     */
    private long executeTime;

    /**
     * 新建一个延迟的事务消息模型，默认延迟 3s
     *
     * @param msgId 事务消息的 id
     * @return 延迟的事务消息模型
     */
    public static DelayMessageModel newDelayTxMsgModel(String msgId) {
        return new DelayMessageModel(SlotMsgTypeEnum.TX_MSG, new DelayTxMessageModel(msgId),
                DelayTimeUtil.getExecuteTime(BrokerConstant.TX_MSG_DELAY_TIME));
    }

    /**
     * 新建一个延迟消息模型
     *
     * @param pushDelayMsgReq 发送延迟消息的请求
     * @return 延迟消息模型
     */
    public static DelayMessageModel newDelayMsgModel(PushDelayMsgReq pushDelayMsgReq) {
        return new DelayMessageModel(SlotMsgTypeEnum.DELAY_MSG, pushDelayMsgReq, pushDelayMsgReq.getExecuteTime());
    }

    /**
     * 新建一个延迟的重试消费消息模型
     *
     * @param retryConsumeMsg 重试消费消息
     * @param executeTime     重试的时间
     * @return 延迟的重试消费消息模型
     */
    public static DelayMessageModel newRetryMsgModel(RetryConsumeMsg retryConsumeMsg, long executeTime) {
        return new DelayMessageModel(SlotMsgTypeEnum.RETRY_MSG, retryConsumeMsg, executeTime);
    }

    public DelayMessageModel() {
    }

    public DelayMessageModel(SlotMsgTypeEnum slotMsgType, Object msg, long executeTime) {
        this.msgType = slotMsgType.getCode();
        this.msg = msg;
        this.executeTime = executeTime;
    }

    public Object getMsg() {
        return msg;
    }

    public void setMsg(Object msg) {
        this.msg = msg;
    }

    public int getMsgType() {
        return msgType;
    }

    public void setMsgType(int msgType) {
        this.msgType = msgType;
    }

    public long getExecuteTime() {
        return executeTime;
    }

    public void setExecuteTime(long executeTime) {
        this.executeTime = executeTime;
    }
}
